package com.drophair.jspwork.bean;

import java.sql.*;

public class Active {
    private String m_code="";
    private String a_code="";

    private static String driverName="com.mysql.cj.jdbc.Driver";
    private static String localHost="127.0.0.1";
    private static String mysqlPort="3306";
    private static String userName="root";
    private static String userPwd="123456";
    private static String dbBase="javadb";
    private static String url=String.format("jdbc:mysql://%s:%s/%s?useSSL=false&serverTimezone=UTC",localHost,mysqlPort,dbBase);

    private String sql="";

    public void setCode(String m){
        this.m_code=m;
    }

    private void setA_code(){
        this.a_code=new StringBuffer(this.m_code).reverse().toString();
    }
    public void save() throws SQLException, ClassNotFoundException {
        Class.forName(driverName);
        Connection connection= DriverManager.getConnection(url,userName,userPwd);
        Statement statement=connection.createStatement();
        sql=String.format("insert into activecode values ('%s','%s');",this.m_code,this.a_code);
        System.out.println(sql);
        int i = statement.executeUpdate(sql);
        connection.close();
    }


    public String getCode() throws SQLException, ClassNotFoundException {
        setA_code();
        save();
        return this.a_code;
    }
}
